/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "lnn_coap_discovery_impl.h"

#include <securec.h>

#include "auth_interface.h"
#include "bus_center_manager.h"
#include "softbus_adapter_crypto.h"
#include "softbus_adapter_mem.h"
#include "softbus_def.h"
#include "softbus_errcode.h"
#include "softbus_log.h"
#include "softbus_utils.h"

#define LNN_DISC_CAPABILITY "ddmpCapability"
#define LNN_SUBSCRIBE_ID 0
#define LNN_PUBLISH_ID 0

#define LNN_SHORT_HASH_LEN 8
#define LNN_SHORT_HASH_HEX_LEN 16

static LnnDiscoveryImplCallback g_callback;

static void DeviceFound(const DeviceInfo *device, const InnerDeviceInfoAddtions *addtions);

static DiscInnerCallback g_discCb = {
    .OnDeviceFound = DeviceFound,
};

NO_SANITIZE("cfi") static void DeviceFound(const DeviceInfo *device, const InnerDeviceInfoAddtions *addtions)
{
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "Device Found(virtual)");
    ConnectionAddr addr;
    DeviceInfo tmpInfo;
    (void) addtions;

    if (device == NULL) {
        SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "device para is null");
        return;
    }

    char *anoyUdid = NULL;
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "DeviceFound devName: %s, devId: %s",
        device->devName, ToSecureStrDeviceID(device->devId, &anoyUdid));
    SoftBusFree(anoyUdid);

    if (device->addr[0].type != CONNECTION_ADDR_WLAN && device->addr[0].type != CONNECTION_ADDR_ETH) {
        SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "discovery get invalid addrtype: %d", device->addr[0].type);
        return;
    }

    if (g_callback.OnDeviceFound) {
        g_callback.OnDeviceFound(&addr);
    }
}

int32_t LnnStartCoapPublish(void)
{
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn start coap publish(virtual)");
    return SOFTBUS_OK;
}

int32_t LnnStopCoapPublish(void)
{
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn stop coap publish(virtual)");
    return SOFTBUS_OK;
}

int32_t LnnStopCoapDiscovery(void)
{
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "coap discovery stop(virtual)!");
    return SOFTBUS_OK;
}

int32_t LnnStartCoapDiscovery(void)
{
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "coap discovery start(virtual)!");
    return SOFTBUS_OK;
}

int32_t LnnInitCoapDiscovery(LnnDiscoveryImplCallback *callback)
{
    SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "coap discovery Init(virtual)!");
    return SOFTBUS_OK;
}
